<template>
  <div class="login_container">
    <div class="login_box">
      <div class="page_logo">
        <img src="../assets/logo.png" class="page_logo_img" />
      </div>
      <el-form
        label-width="0px"
        class="form_login"
        :model="loginForm"
        :rules="loginRules"
        ref="userInput"
      >
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn_login">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        // 表单验证规则
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '用户名长度不对', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度不对', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.userInput.validate(valid => {
        if (!valid) return
        this.$http.post('login', this.loginForm).then(res => {
          // console.log(res) 账号 admin 密码 123456
          if (res.data.meta.status !== 200) {
            this.$message({
              message: '登陆失败',
              type: 'error'
            })
          } else {
            this.$message({
              message: '登陆成功',
              type: 'success'
            })
            // 保存token
            console.log(res)
            window.sessionStorage.setItem('token', res.data.data.token)
            this.$router.push('/home')
          }
        })
      })
    },
    reset() {
      this.$refs.userInput.resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .page_logo {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      .page_logo_img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .form_login {
      position: absolute;
      width: 100%;
      bottom: 0;
      padding: 0 10px;
      box-sizing: border-box;
    }
    .btn_login {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
